#!/bin/sh

#########################################################
# version: 0.1 beta
# ###Copy  by ture2###############
# Requirement: You need to have Debian or UBUNTU.
# home dir#############################################
cd ~
username=$(whoami)
groupname=$username
hostname1=$(hostname)
# version
Version_id='2.11.1'
# value   [online | offline]
Version_line='offline'

# The domain name and CA info.
My_domain=$hostname1
TLS_C='CN'
TLS_ST='Beijing'
TLS_L='Beijing'
TLS_O='example'
TLS_OU='Personal'
TLS_CN='MyPersonal'


# online version tag
# download for github or local.
Online=true
file_name="harbor-${Version_line}-installer-v${Version_id}.tgz"

# check wgt/curl
if ! command -v wget >/dev/null 2>&1; then
    sudo apt-get install wget curl -y
fi

echo $file_name

# Download harbor
if [ Online ]; then
   echo 'Downloading  harbor.'
   wget -c https://github.com/goharbor/harbor/releases/download/v${Version_id}/harbor-${Version_line}-installer-v${Version_id}.tgz
   echo 'Download harbor END. '
fi

if  [ -f "$file_name" ]; then
    echo "file:$file_name ok!"
else
    echo  "File not found."
    exit  0
fi
# tar harbor
tar xzvf $file_name
cd ~/harbor/
sudo cp harbor.yml.tmpl harbor.yml

# ENV Setting
if [ -d "/etc/modules" ]; then
   mkdir -p /etc/modules
   echo "kvm" | sudo tee -a /etc/modules
fi

if cat /proc/cpuinfo | grep -q 'Intel'; then
   echo "Intel CPU kvm Setting."
   echo "kvm_intel" | sudo tee -a /etc/modules
elif cat /proc/cpuinfo | grep -q 'AMD'; then
   echo "AMD CPU kvm setting."
   echo "kvm_intel" | sudo tee -a /etc/modules
fi

#  uninstall all conflicting packages:
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done
# Add Docker's official GPG key
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
if [ -f "/etc/apt/keyrings/docker.asc" ]; then
        echo 'Docker asc file ok.'
else
        echo 'Restart download Docker asc file.'
        sudo wget -c  https://download.docker.com/linux/ubuntu/gpg -O /etc/apt/keyrings/docker.asc
fi
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

# Install Docker-eng
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
sudo  ln -s /usr/libexec/docker/cli-plugins/docker-compose /usr/bin/docker-compose
cd ~/harbor
# HTTP  tag , 0 : not chang; 1 :  https ; 2 :http;   default 1
harbor_http=1
echo $harbor_http
if [ $harbor_http -eq 1 ]; then
   sudo openssl genrsa -out ca.key 4096

   sudo openssl req -x509 -new -nodes -sha512 -days 3650 \
    -subj "/C=${TLS_C}/ST=${TLS_ST}/L=${TLS_L}/O=${TLS_O}/OU=${TLS_OU}/CN=${TLS_CN} Root CA" \
    -key ca.key \
    -out ca.crt

   sudo openssl genrsa -out ${My_domain}.key 4096

   sudo openssl req -sha512 -new \
    -subj "/C=${TLS_C}/ST=${TLS_ST}/L=${TLS_L}/O=${TLS_O}/OU=${TLS_OU}/CN=${TLS_CN}" \
    -key ${My_domain}.key \
    -out ${My_domain}.csr


     cat > v3.ext <<-EOF
        authorityKeyIdentifier=keyid,issuer
        basicConstraints=CA:FALSE
        keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
        extendedKeyUsage = serverAuth
        subjectAltName = @alt_names
        [alt_names]
          DNS.1= ${My_domain}
          DNS.2=localhost
          DNS.3=$hostname1
          IP.1=127.0.0.1
EOF

    sudo openssl x509 -req -sha512 -days 3650 \
    -extfile v3.ext \
    -CA ca.crt -CAkey ca.key -CAcreateserial \
    -in ${My_domain}.csr \
    -out ${My_domain}.crt
    sudo mkdir -p /data/cert/
    sudo chown -R   ${username}:${groupname} /data/cert/
    sudo cp ${My_domain}.crt /data/cert/
    sudo cp ${My_domain}.key /data/cert/

    sudo openssl x509 -inform PEM -in ${My_domain}.crt -out ${My_domain}.cert
    sudo mkdir -p /etc/docker/certs.d/${My_domain}
    sudo cp ${My_domain}.cert /etc/docker/certs.d/${My_domain}
    sudo cp ${My_domain}.key /etc/docker/certs.d/${My_domain}
    sudo cp ca.crt /etc/docker/certs.d/${My_domain}
    sed -i "s/certificate: \/your\/certificate\/path/certificate: \/data\/cert\/${My_domain}.crt/" ~/harbor/harbor.yml
    sed -i "s/private_key: \/your\/private\/key\/path/private_key: \/data\/cert\/${My_domain}.key/" ~/harbor/harbor.yml

   sudo openssl genrsa -out ${My_domain}_01.key 4096
   sudo openssl req -sha512 -new \
    -subj "/C=${TLS_C}/ST=${TLS_ST}/L=${TLS_L}/O=${TLS_O}/OU=${TLS_OU}/CN=${TLS_CN}" \
    -key ${My_domain}_01.key \
    -out ${My_domain}_01.csr
   sudo openssl x509 -req -sha512 -days 3650 \
    -extfile v3.ext \
    -CA ca.crt -CAkey ca.key -CAcreateserial \
    -in ${My_domain}_01.csr \
    -out ${My_domain}_01.crt
    sudo cp ${My_domain}_01.key /etc/harbor/tls/internal/harbor_internal_ca.key
    sudo cp ${My_domain}_01.crt /etc/harbor/tls/internal/harbor_internal_ca.crt
    sudo mkdir -p /etc/harbor/tls/internal
    sudo chown -R ${username}:${groupname} /etc/harbor/tls/internal
    sed -i '/^#.*internal\_tls:$/s/^#/ /'  ~/harbor/harbor.yml
    sed -i '35 s/#//' ~/harbor/harbor.yml
    sed -i '/^#.*dir: \/etc\/harbor\/tls\/internal$/s/^#//'  ~/harbor/harbor.yml
fi

if [ $harbor_http -eq 2 ]; then
    sed -i 's/https:/#https:/' ~/harbor/harbor.yml
    sed -i 's/  port: 443/#&/g' ~/harbor/harbor.yml
    sed -i 's/  certificate:/#&/g' ~/harbor/harbor.yml
    sed -i 's/  private_key:/#&/g' ~/harbor/harbor.yml
fi

# set domain
sed -i "s/hostname: reg.mydomain.com/hostname: ${My_domain}/" ~/harbor/harbor.yml
echo $(pwd)
# Harbor install --with-trivy
sudo ~/harbor/prepare.sh  --with-trivy
sudo ~/harbor/install.sh  --with-trivy

# END
echo "Good Luck!"
